package in.googydiary.service.test;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

// Implements Filter class
public class LogFilter implements Filter  {
   public void  init(FilterConfig config) 
                         throws ServletException{
      // Get init parameter 
      String testParam = config.getInitParameter("test-param"); 

      //Print the init parameter 
      //System.out.println("Test Param: " + testParam); 
   }
   public void  doFilter(ServletRequest request, 
                 ServletResponse response,
                 FilterChain chain) 
                 throws java.io.IOException, ServletException {
   
      // Get the IP address of client machine.   
     // String ipAddress = request.getRemoteAddr();

      // Log the IP address and current timestamp.
      //System.out.println("Pre Process");
      // Pass request back down the filter chain
	   PrintWriter out = response.getWriter();
	   CharResponseWrapper wrapper = new CharResponseWrapper(
	       (HttpServletResponse)response);
      chain.doFilter(request,wrapper);
      //System.out.println("POST Process:   "+response.getContentType());
      //PrintWriter out = response.getWriter();
      //response.flushBuffer();
      //response.setContentType("application/json");
      /*out.print("SU");
      out.flush();
      out.close();*/
     //;
      int count = 2;
      
      if (count >= 0){	      
	      CharArrayWriter caw = new CharArrayWriter();
	      String output= "{rowcount:12}";
	      caw.write(output);
	      response.setContentLength(caw.toString().getBytes().length);
	      response.setContentType("application/json");
	      out.write(caw.toString());
	      out.close();
      }
      
   }
   public void destroy( ){
      /* Called before the Filter instance is removed 
      from service by the web container*/
   }
}